﻿using GameEngine.Clients.Silverlight.View;
using MySettlers.Screens;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using GameEngine.View;
using System.Collections.Generic;
using GameEngine.Model.Data;
using System.Windows.Controls;
using System.IO;

namespace MySettlers.Commands
{
    /// <summary>
    /// 
    /// </summary>
    public class Page_EditorMainMenu_In_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            var mediator = MyViewFacade.Instance.CreateMenuMediator<string, EditorMainMenu>(
                MyFacade.EditorPageMediatorName, 
                new EditorMainMenu(), 
                MyFacade.PAGE_EDITORMAINMENU_OUT_COMMAND);
            MyFacade.Instance.RegisterMediator(mediator);
            MyFacadeHelper.DesktopNavigate(mediator.ViewComponent);
        }
    }

    public class Page_EditorMainMenu_Out_Command : SimpleCommand
    {
        override public void Execute(INotification notification)
        {
            bool exit = false;
            switch (notification.Body as string)
            {
                case EditorMainMenu.BACKBUTTON:
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_MAIN_IN_COMMAND);
                    exit = true;
                    break;
                case EditorMainMenu.NEWGAMEBUTTON:
                    MyFacade.Instance.SendNotification(MyFacade.PAGE_EDITORNEWGAME_IN_COMMAND);
                    exit = true;
                    break;
                case EditorMainMenu.EDITGAMEBUTTON:      
                    OpenFileDialog ofd = new OpenFileDialog();
                    if (ofd.ShowDialog().Value)
                    {
                        TextReader w = new StreamReader(ofd.File.OpenRead());
                        string gameSerialized = w.ReadToEnd();
                        w.Close();
                        var game = MyModel.Instance.CreateGameVO();
                        game.DeSerialize(gameSerialized);

                        MyFacade.Instance.SendNotification(MyFacade.PAGE_EDITORLOADGAME_IN_COMMAND, game);
                        exit = true;
                    }
                    break;
            }
            if(exit)
                MyFacade.Instance.RemoveMediator(MyFacade.EditorPageMediatorName);
        }
    }
}
